﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using BasicForms;
using Lists;
using DevExpress.XtraPrinting;
using System.Collections;
using CustomSecurity;

namespace ReportGenerators.AccoutingXaLa
{
    public partial class FrmBangCanDoiTaiKhoan : BasicForms.FrmSupportPrintableComponents
    {
        public FrmBangCanDoiTaiKhoan()
        {
            InitializeComponent();
        }

        AccoutingXaLa.DsAccountingXaLa dsDetail = new AccoutingXaLa.DsAccountingXaLa();

        private void FrmBangCanDoiTaiKhoan_Load(object sender, EventArgs e)
        {
            if (DesignMode) return;

            cmbKyBaoCao.SelectedIndex = 19;

            FormatNumberInGridView(bandedGridView1);
            foreach (DevExpress.XtraGrid.Views.BandedGrid.BandedGridColumn column in bandedGridView1.Columns)
            {
                column.OptionsFilter.AutoFilterCondition = DevExpress.XtraGrid.Columns.AutoFilterCondition.Contains;
                //if(column.Name != "colSo" || column.Name != "colTenDoiTuongNghiepVu")
                //    column.OptionsColumn.AllowMerge = DevExpress.Utils.DefaultBoolean.False;
            }
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            BasicForms.FrmProgressBarDev.StartShowProgress();
            SqlConnection con = new SqlConnection(GeneralLibs.LocalSettings.ConnectString);
            dsDetail.Detail_BangCanDoiTK.Clear();
            
            int stt = 1;
            try
            {
                con.Open();
                SqlCommand cmd = con.CreateCommand();

                //string strCondition = "SoHieu IN ('1111', '1121', '1211', '1311', '141', '211', '214', '242', '311', '331', '333', '334', '3532', '411', '418', '421', '511', '632', '642')";
                DanhMucTaiKhoan dmTK = DanhMucTaiKhoan.GetAll();
              
                foreach (TaiKhoan tk in dmTK)
	            {
                    Calculate(cmd, tk, stt++);
	            }

                gridControl.DataSource = dsDetail.Detail_BangCanDoiTK;
            }
            catch (Exception ex)
            {
                BasicForms.FrmExceptionNotifier.ShowAndLog(ex);
            }
            finally
            {
                con.Close();
                con = null;
                FormatNumberInGridView(bandedGridView1);
            }
            BasicForms.FrmProgressBarDev.FinishShowProgress();

        }

        private void Calculate(SqlCommand cmd, TaiKhoan tk, int stt)
        {
            try
            {
                SoDuTaiKhoan soDuBanDau = null;
                SoDuTaiKhoan soDuPhatSinhTrongKy = null;
                SoDuTaiKhoan soDuCuoiKy = null;

                soDuBanDau = SoDuTaiKhoan.CalculateFromDB(cmd, dtNgayBatDau.DateTime, tk.ID);
                soDuPhatSinhTrongKy = SoDuTaiKhoan.CalculatePhatSinhTrongKyFromDB(cmd, dtNgayBatDau.DateTime, dtNgayKetThuc.DateTime, tk.ID);
                soDuCuoiKy = SoDuTaiKhoan.CalculateFromDB(cmd, dtNgayKetThuc.DateTime, tk.ID);

                DataRow row = dsDetail.Detail_BangCanDoiTK.NewRow();
                row["STT"] = stt;
                row["TenTaiKhoan"] = tk.Ten;
                row["SoHieu"] = tk.SoHieu;
                row["NoDauKy"] = soDuBanDau.DuNo;
                row["CoDauKy"] = soDuBanDau.DuCo;
                row["NoTrongKy"] = soDuPhatSinhTrongKy.DuNo;
                row["CoTrongKy"] = soDuPhatSinhTrongKy.DuCo;
                row["NoCuoiKy"] = soDuCuoiKy.DuNo;
                row["CoCuoiKy"] = soDuCuoiKy.DuCo;
                dsDetail.Detail_BangCanDoiTK.Rows.Add(row);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void cmbKyBaoCao_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                FrmThamSoBaoCao.ThietLapKhoangThoiGianBaoCao(cmbKyBaoCao.Text, cmbKyBaoCao, dtNgayBatDau, dtNgayKetThuc);
            }
            catch (Exception ex)
            {
                BasicForms.FrmExceptionNotifier.ShowAndLog(ex);
            }
        }

        private void btnPreview_Click(object sender, EventArgs e)
        {
            try
            {
                string year = "";
                if (dtNgayBatDau.DateTime.Year == dtNgayBatDau.DateTime.Year) year = dtNgayBatDau.DateTime.Year.ToString();
                else year = dtNgayBatDau.DateTime.Year.ToString() + dtNgayKetThuc.DateTime.Year.ToString();

                string curUSer = "";
                if(User.CurUser.FullName != null)
                    curUSer = User.CurUser.FullName;

                PrintWithHeaderAnd4Footer(true, gridControl, true, true, string.Format("BẢNG CÂN ĐỐI TÀI KHOẢN NĂM {0}", year), "Đơn vị tính: Đồng", "RIGHT", "", "", 2,
                    "Người lập biểu", curUSer, string.Format("Hà Nội, ngày {0} tháng {1} năm {2}",
                    DateTime.Now.Day, DateTime.Now.Month, DateTime.Now.Year), "HTXNN XA LA");
            }
            catch (Exception ex)
            {
                BasicForms.FrmExceptionNotifier.ShowAndLog(ex);
            }
        }
    }
}
